package com.serenegiant.usbcameratest4.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.serenegiant.usb.UVCCamera;

import static android.content.Context.POWER_SERVICE;

public class SystemUtils {

    private static final String TAG = SystemUtils.class.getSimpleName();
    /**
     * 忽略电池优化
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    public static void ignoreBatteryOptimization( Context context ) {
        try{
            PowerManager powerManager = (PowerManager)context.getSystemService( POWER_SERVICE );
            boolean hasIgnored = powerManager.isIgnoringBatteryOptimizations( context.getPackageName());
            if(!hasIgnored) {
                Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:"+context.getPackageName()));
                context.startActivity(intent);
            }
        }catch(Exception e){
            //TODO :handle exception
            Log.e("ex",e.getMessage());
        }
    }


}
